﻿using Microsoft.Extensions.Configuration;
using Digitalmes.Core;

namespace Digitalmes.Infrastructure.Tests;

/// <summary>
/// 测试治具基类。
/// </summary>
public abstract class BaseTestFixture : IDisposable
{
    private readonly IHost _host;

    /// <summary>
    /// 获取服务。
    /// </summary>
    public IServiceProvider Services { get; }

    /// <summary>
    /// 获取配置
    /// </summary>
    public IConfiguration Configuration => Services.GetRequiredService<IConfiguration>();

    public BaseTestFixture()
    {
        _host = Host.CreateDefaultBuilder()
            .ConfigureServices((hostBuilder, services) => ConfigureServices(services, hostBuilder.Configuration))
            .UseCoreSetup()
            .UseInfrastructureSetup()
            .Build();

        _host.Start();

        Services = _host.Services;
    }

    /// <summary>
    /// 配置自定义的服务。
    /// </summary>
    /// <param name="services"></param>
    /// <param name="configuration"></param>
    public virtual void ConfigureServices(IServiceCollection services, IConfiguration configuration)
    {
    }

    public virtual void Dispose()
    {
        _host.Dispose();
    }
}
